package com.vancouver.knows.ui.activity.userpost;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.business.task.mark.CommentReportTaskMark;
import com.vancouver.knows.ui.activity.login.LoginActivity;
import com.vancouver.knows.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-07-25.
 */
public class PostReportActivity extends BaseActivity implements IResultReceiver {

    public static final String POST_ID_EXTRA = "post_id_extra";

    @Bind(R.id.report_list)
    RecyclerView mReportList;

    private ReportListAdapter mReportListAdapter;
    private String mPostId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_report);

        Intent intent = getIntent();
        if (intent == null) {
            return;
        }

        mPostId = intent.getStringExtra(POST_ID_EXTRA);
        if (mPostId == null) {
            return;
        }

        mReportList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mReportListAdapter = new ReportListAdapter(this, getResources().getStringArray(R.array.report_contents));
        mReportList.setAdapter(mReportListAdapter);

    }

    @OnClick(R.id.back_icon)
    public void onBackButtonClicked() {
        onBackPressed();
    }

    @OnClick(R.id.go_button)
    public void onGoButtonClicked() {
        String userId = imContext.getSharedPrefManager().getUserId();
        if (userId == null) {
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        CommentReportTaskMark commentReportTaskMark = imContext.getBusinessModule().getTaskMarkPool().getCommentReportTaskMark();
        String reportContent = mReportListAdapter.getSelected();
        if (reportContent != null) {
            imContext.getBusinessModule().getServiceWrapper().commentReport(this, commentReportTaskMark, userId, mPostId, "P", reportContent);
        }
    }

    @Override
    public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
        if (exception == null) {
            finish();
        } else {
            Toast.makeText(this, exception.getExMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    public static class ReportListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private LayoutInflater mLayoutInflater;
        private String[] mContents;
        private int mChosenPosition = -1;

        public ReportListAdapter(Context context, String[] contents) {
            this.mLayoutInflater = LayoutInflater.from(context);
            this.mContents = contents;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = mLayoutInflater.inflate(R.layout.view_radio_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            String city = mContents[position];
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.title.setText(city);

            if (position == mChosenPosition) {
                viewHolder.icon.setImageResource(R.drawable.checkmark);
                viewHolder.icon.setBackgroundResource(R.drawable.checkmark_selected);
            } else {
                viewHolder.icon.setImageResource(0);
                viewHolder.icon.setBackgroundResource(R.drawable.checkmark_unselected);
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mChosenPosition = viewHolder.getAdapterPosition();
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return mContents.length;
        }

        public String getSelected() {
            if (mChosenPosition == -1) {
                return null;
            }

            return mContents[mChosenPosition];
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {

            @Bind(R.id.icon)
            ImageView icon;
            @Bind(R.id.title)
            TextView title;


            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
